package library.database.user;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// import com.mysql.cj.Session;
// import com.mysql.cj.protocol.Resultset;

public class DatabaseUserProxy extends DatabaseProxy {
	protected PreparedStatement ps;
	private Connection conn=getConnection();
	public DatabaseUserProxy() throws ClassNotFoundException, IOException, SQLException {
		super();
	}
	/**
	 * 用户登录
	 * @param id 
	 * @param password 
	 * @return bool 用户登陆失败返回false,用户登录成功返回true
	 * @throws SQLException
	 * @right both
	 * @tips:可对外被代理调用
	 */
	public boolean userLogin(int id, String password) throws SQLException {
		ResultSet rs = seek(id);
		while(rs.next())
		{
			java.lang.String realpas = rs.getString(3);
			if(!password.equals(realpas))
				{
					return false;
				}
		}
		return true;
	}
	/**
	 * 寻找用户是否存在
	 * @param id
	 * @return 若存在则返回true 若不存在则返回false
	 * @throws SQLException
	 * @right both
	 * @tips 不可对外被调用
	 */
	public boolean isExisted(int id) throws SQLException {
		ResultSet rs = seek(id);
		if(rs.next()==false) return false;
		return true;
		}
	/**
	 * 新增读者信息  
	 * @return bool 成功返回true 反之返回false
	 * @throws SQLException 
	 * @right both 
	 */
	public boolean insert(String name,String password,String email,String dep) throws SQLException {
		try{
			conn.setAutoCommit(false);
			java.lang.String sql = "INSERT INTO user values(?,?,?,?,?,0);";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1,name);
			ps.setInt(2,0);
			ps.setString(3,password);
			ps.setString(4,email);
			ps.setString(5,dep);
			ps.execute();
			conn.commit();
			return true;
		}catch(Exception e){
			conn.rollback();
			e.printStackTrace();
			return false;
		}

	}
	/**
	 * 查找读者信息
	 * @param id
	 * @return ResultSet,如果rs.next()==false 则不存在该读者
	 * @throws SQLException
	 */
	public ResultSet seek(int id) throws SQLException {
		try{
			conn.setAutoCommit(false);
			java.lang.String sql = "SELECT * From user where id = ?;";
			ps = conn.prepareStatement(sql);
			ps.setInt(1,id);
			ResultSet rs = ps.executeQuery();
			conn.commit();
			return rs;
		}catch(Exception e){
			conn.rollback();
			e.printStackTrace();
			return null;
		}

	}
	/**
	 * 修改读者信息 
	 * @return 
	 * @throws SQLExint
	 */
	public boolean modify(int func, String info, int id) throws SQLException { 
		boolean flag1 = isExisted(id);
		if(flag1 == false) {
			return false;
		}
		try{
			conn.setAutoCommit(false);
			java.lang.String sql = null;
			switch(func) {
				case 1: 
					sql ="UPDATE user set name = ? Where id = ?;"; //修改读者名字
					break;
				case 2:
					sql ="UPDATE user set password = ? where id = ?;";//修改读者密码
					break;
				case 3:
					sql = "UPDATE user set email= ? where id = ?;";//修改读者的邮箱地址
					break;
				case 4:
					sql ="UPDATE user set department=? where id = ?;";//需改读者部门
					break;
				case 5:
					sql ="UPDATE user set state=? where id = ?;";//修改读者的权限
					break;
		}
			ps = conn.prepareStatement(sql);
			if(func!=5)
				ps.setString(1, info);
			else
				ps.setInt(1, 1);
			ps.setInt(2, id);
			ps.execute(); 
			conn.commit();
			return true;
		}catch(Exception e){
			conn.rollback();
			e.printStackTrace();
			return false;
		}	
}
	public ResultSet getRegId(){
		try{
			conn.setAutoCommit(false);
			String sql = "SELECT max(id) From user as maxid;";
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			conn.commit();
			return rs;
		}catch(Exception e){
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return null;
			}
			e.printStackTrace();
			return null;
		}
	}
}
